package com.bankcomm.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bankcomm.BankcommApplication;
import com.bankcomm.R;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.views.BCMHomeView;
import com.bankcomm.util.LPUtils;
import com.rytong.tools.animation.Rotate3dAnimation;
import com.rytong.tools.ui.WaitProgressDialog;

/**
 * BCMNewsCenter.java
 * @author cheng.l 2013-6-13 下午01:42:03
 */
public class BCMEditionChange extends BaseViewCopy implements OnClickListener{

	private ImageView versionImage = null;
	private Button versionButton = null;
	private Rotate3dAnimation rotate3D = null;
	private Rotate3dAnimation rotate3D1 = null;
	
	private WaitProgressDialog wpd = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.boc_edition_change);
		
		
		versionImage = (ImageView)findViewById(R.id.image_edition);
		versionButton = (Button)findViewById(R.id.button_edition);
		
		versionButton.setOnClickListener(this);
		
		Button tl = (Button)findViewById(R.id.newsTitleL);
		Button tr = (Button)findViewById(R.id.newsTitleR);
		
		LPUtils.scaledTHeight((LinearLayout)tl.getParent());
		LPUtils.setTitleButtonWH(tl);
		LPUtils.setTitleButtonWH(tr);
		
		tl.setOnClickListener(this);
		tr.setOnClickListener(this);
		
		if(null == wpd){
			wpd = new WaitProgressDialog(this, "版本切换中...");			
		}
		reset();
	}
	
	private AnimationListener listener = new AnimationListener() {
		
		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			reset();
			versionImage.startAnimation(rotate3D1);
		}
	};
	
	private AnimationListener listener2 = new AnimationListener() {
		
		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			finishActivity();
		}
	};
	/**
	 * 重置当前页面
	 */
	public void reset(){
		String version = LPMid.getInstance().read("version");
		if(null != version && version.equalsIgnoreCase("university")){
			versionImage.setImageResource(R.drawable.version_university);
			versionButton.setText(R.string.edition_normal);
		}else{
			versionImage.setImageResource(R.drawable.version_nomral);
			versionButton.setText(R.string.edition_university);
		}
	}
	
	/**
	 * 重置选择城市的数据
	 */
	public void resetData(){
		String version = LPMid.getInstance().read("version");
		if(null != version && version.equalsIgnoreCase("university")){
			LPMid.getInstance().saveToSP("version", "normal");
			LPMid.getInstance().um_.data.parseCitys();
		}else{
			LPMid.getInstance().saveToSP("version", "university");
			LPMid.getInstance().um_.data.readProvince();
		}
		BCMHomeView.mSelectCity.resetGroups();
	}
	
	public void initRoateAnimation(){
		if(null == rotate3D){
			rotate3D = new Rotate3dAnimation(0, 90, versionImage.getWidth() >> 1, 0, 0,false, "y");
			rotate3D.setDuration(500);
			rotate3D.setFillAfter(true);			
			rotate3D.setAnimationListener(listener);
		}
		
		if(null == rotate3D1){
			rotate3D1 = new Rotate3dAnimation(-90, 0, versionImage.getWidth() >> 1, 0, 0,false, "y");
			rotate3D1.setDuration(500);
			rotate3D1.setFillAfter(true);
			rotate3D1.setAnimationListener(listener2);
		}
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.newsTitleL:
			finishActivity();
			break;
		case R.id.newsTitleR:
			finishActivity();
			break;
		case R.id.button_edition:
			handler.sendEmptyMessage(0);
			changeVersion();
			break;
		}
	}
	
	public void changeVersion(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				resetData();
				handler.sendEmptyMessage(1);
			}
		}).start();
	}
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch(msg.what){
			case 0:
				wpd.show(null);
				break;
			case 1:
				BCMHomeView.mSelectCity.resetView();
				BCMHomeView.mSelectCity.bcmHome.setTitle();
				//initRoateAnimation();
				wpd.dismiss();
				//versionImage.startAnimation(rotate3D);
				finishActivity();
				break;
			}
			super.handleMessage(msg);
	}};

	public void finishActivity(){
		BankcommApplication.getInstance().removeActivity(this);
		this.finish();
	}
}
